//
//  ILGCustomOperation.m
//  ILGDemo
//
//  Created by happy on 2020/4/6.
//  Copyright © 2020 Happy. All rights reserved.
//

#import "ILGCustomOperation.h"

@implementation ILGCustomOperation
{
    BOOL _myExecuting;
    BOOL _myFinished;
}


#pragma mark - Optional Method

- (void)main
{
    NSLog(@"%s : ZhangSan", __func__);
    
    [self willChangeValueForKey:@"isExecuting"];
    _myExecuting = NO;
    [self didChangeValueForKey:@"isExecuting"];
    
    [self willChangeValueForKey:@"isFinished"];
    _myFinished = YES;
    [self didChangeValueForKey:@"isFinished"];
}


#pragma mark - Required Method

- (void)start
{
    if ([self isCancelled])
    {
         // Must move the operation to the finished state if it is canceled.
        [self willChangeValueForKey:@"isFinished"];
        _myFinished = YES;
        [self didChangeValueForKey:@"isFinished"];
        return;
    }
    
    // If the operation is not canceled, begin executing the task.
    [self willChangeValueForKey:@"isExecuting"];
    [NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
    _myExecuting = YES;
    [self didChangeValueForKey:@"isExecuting"];
}

- (BOOL)isAsynchronous
{
    return YES;
}

// 此方法应该保证线程安全
- (BOOL)isExecuting
{
    return _myExecuting;
}

// 此方法应该保证线程安全
- (BOOL)isFinished
{
    return _myFinished;
}

@end
